﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TrainingRemarkRepo
    {

        public const string m_msg_head = "TrainingRemark";

        public IEnumerable<TrainingRemarkModels> GetTrainingRemark()
        {
            List<TrainingRemarkModels> TrainingRemarks = new List<TrainingRemarkModels>();
            string SQLCommand = @"select TBTrainingRemark.*, TBTitleOnsiteTraning.TitleOnsiteTraningName 
                                  from TBTrainingRemark LEFT JOIN TBTitleOnsiteTraning ON TBTrainingRemark.TitleOnsiteTraningId = TBTitleOnsiteTraning.TitleOnsiteTraningId 
                                  WHERE TBTrainingRemark.Status <> 'Delete' 
                                  Order BY TBTrainingRemark.TitleOnsiteTraningId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TrainingRemarkModels TrainingRemark = new TrainingRemarkModels();
                        TrainingRemark.Id = (int)reader["Id"];
                        TrainingRemark.TitleOnsiteTraningId = (int)reader["TitleOnsiteTraningId"];
                        TrainingRemark.TitleOnsiteTraningName = (string)reader["TitleOnsiteTraningName"];
                        TrainingRemark.TrainingRemarkTh = (string)reader["TrainingRemarkTh"];
                        TrainingRemark.TrainingRemarkEng = (string)reader["TrainingRemarkEng"];
                        TrainingRemark.Status = (string)reader["Status"];
                        TrainingRemark.CreateBy = (string)reader["CreateBy"];
                        TrainingRemark.CreateOn = (DateTime)reader["CreateOn"];
                        TrainingRemark.UpdateBy = (string)reader["UpdateBy"];
                        TrainingRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        TrainingRemarks.Add(TrainingRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TrainingRemarks.ToList();
        }

        public TrainingRemarkModels GetTrainingRemarkById(int id)
        {
            TrainingRemarkModels TrainingRemark = new TrainingRemarkModels();
            string SQLCommand = @"select TBTrainingRemark.*, TBTitleOnsiteTraning.TitleOnsiteTraningName 
                                  from TBTrainingRemark LEFT JOIN TBTitleOnsiteTraning ON TBTrainingRemark.TitleOnsiteTraningId = TBTitleOnsiteTraning.TitleOnsiteTraningId where TBTrainingRemark.Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TrainingRemark.Id = (int)reader["Id"];
                        TrainingRemark.TitleOnsiteTraningId = (int)reader["TitleOnsiteTraningId"];
                        TrainingRemark.TitleOnsiteTraningName = (string)reader["TitleOnsiteTraningName"];
                        TrainingRemark.TrainingRemarkTh = (string)reader["TrainingRemarkTh"];
                        TrainingRemark.TrainingRemarkEng = (string)reader["TrainingRemarkEng"];
                        TrainingRemark.Status = (string)reader["Status"];
                        TrainingRemark.CreateBy = (string)reader["CreateBy"];
                        TrainingRemark.CreateOn = (DateTime)reader["CreateOn"];
                        TrainingRemark.UpdateBy = (string)reader["UpdateBy"];
                        TrainingRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TrainingRemark;
        }

        public bool Create(TrainingRemarkModels TrainingRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTrainingRemark 
                                    values (@TitleOnsiteTraningId,@TrainingRemarkTh,@TrainingRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleOnsiteTraningId", TrainingRemark.TitleOnsiteTraningId);
                    db.AddParameter("@TrainingRemarkTh", TrainingRemark.TrainingRemarkTh);
                    db.AddParameter("@TrainingRemarkEng", TrainingRemark.TrainingRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", TrainingRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TrainingRemarkModels TrainingRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBTrainingRemark 
                                    Set TitleOnsiteTraningId= @TitleOnsiteTraningId,
                                        TrainingRemarkTh=@TrainingRemarkTh,
                                        TrainingRemarkEng=@TrainingRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", TrainingRemark.Id);
                    db.AddParameter("@TitleOnsiteTraningId", TrainingRemark.TitleOnsiteTraningId);
                    db.AddParameter("@TrainingRemarkTh", TrainingRemark.TrainingRemarkTh);
                    db.AddParameter("@TrainingRemarkEng", TrainingRemark.TrainingRemarkEng);
                    db.AddParameter("@UserID", TrainingRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TrainingRemarkModels TrainingRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBTrainingRemark 
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", TrainingRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", TrainingRemark.UpdateBy );

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}